package com.myxy.mylogin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        toReg();
    }

    private void toReg() {

        EditText admin = findViewById(R.id.edit_admin);//手机号
        EditText pswd = findViewById(R.id.edit_pswds);//密码
        EditText pswd2 = findViewById(R.id.edit_pswd2);//确认密码
        EditText com = findViewById(R.id.edit_com);//邮箱
        Button toRegister = findViewById(R.id.toreg);//注册按钮

        toRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(admin.getText())) {
                    //气泡提示
                    Toast.makeText(RegisterActivity.this,
                            "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pswd.getText()) || TextUtils.isEmpty(pswd2.getText())) {
                    //气泡提示
                    Toast.makeText(RegisterActivity.this,
                            "密码或确认密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(com.getText())) {
                    //气泡提示
                    Toast.makeText(RegisterActivity.this,
                            "邮箱不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (admin.getText().toString().length() == 11) {

                    if (pswd.getText().toString().length() >= 8 && pswd.getText().toString().length() <= 16) {
                        if (pswd2.getText().toString().equals(pswd.getText().toString())) {
                            //
                            if (com.getText().toString().contains("@") && com.getText().toString().contains(".") && com.getText().toString().indexOf("@") < com.getText().toString().indexOf(".")) {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                                SharedPreferences sp = getSharedPreferences("app_login", Context.MODE_PRIVATE);

                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("user_admin",admin.getText().toString());
                                editor.putString("user_pwd", pswd2.getText().toString());
                                editor.putString("user_com", com.getText().toString());

                                //3.提交到SharedPreferences文件
                                //apply();为异步提交，不影响主线程
                                editor.apply();
                                //创建Intent
                                Intent intent = new Intent(
                                        RegisterActivity.this, LoginActivity.class);
                                startActivity(intent);
                        } else {
                            Toast.makeText(RegisterActivity.this,
                                    "邮箱格式错误", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else {
                        Toast.makeText(RegisterActivity.this,
                                "两次密码不一致", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    Toast.makeText(RegisterActivity.this,
                            "密码必须8-16位", Toast.LENGTH_SHORT).show();
                    return;
                }
                } else {
                    Toast.makeText(RegisterActivity.this,
                            "手机号必须11位", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }
}